package com.taxshare.purchaser.service.rulesReview;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.entity.InvoiceBuyerRulesEntity;
import com.taxshare.purchaser.entity.InvoiceCollectionEntity;
import com.taxshare.purchaser.mapper.InvoiceBuyerRulesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购方规则检验
 *
 * @author hjh
 */
@Component
public class BuyerReviewCheck implements RuleMainHandler {

    @Autowired
    InvoiceBuyerRulesMapper buyerRulesMapper;

    /**
     * 购方规则检验
     *
     * @param bean 发票实体
     * @param pid  审核规则id
     * @return
     */
    @Override
    public CheckInfoRespVO checkRule(InvoiceCollectionEntity bean, String pid) {
        InvoiceBuyerRulesEntity info = getInfo(pid);
        CheckInfoRespVO vo = new CheckInfoRespVO();
        //审核规则不为空
        if (info != null) {
            //购方抬头拆分
            if (StrUtil.isNotBlank(info.getBuyerSensitiveHead())) {
                List<String> list = Arrays.stream(info.getBuyerSensitiveHead().split(",")).collect(Collectors.toList());
                for (String s : list) {
                    if (bean.getPurchaserName().contains(s)) {
                        //票据类型相同或为全部
                        Boolean flg = checkInvoiceType(info, bean);
                        if (flg) {
                            vo.setGoing(false);
                            vo.setFlag(false);
                            vo.setMsg(info.getBuyerNotPassReasons());
                        }
                    }
                }
            }
        }
        return vo;
    }

    @Override
    public CheckInfoRespVO checkSubmit(String id) {
        CheckInfoRespVO vo = new CheckInfoRespVO();
        InvoiceBuyerRulesEntity info = getInfo(id);
        if (info!=null&&CodeConstant.COMM_CODE_1.equals(info.getForceFlag())){
            vo.setGoing(false);
            vo.setMsg("特殊购方信息规则强制校验不通过!");
        }
        return vo;
    }

    /**
     * 校验发票类型
     * @param info
     * @param bean
     * @return
     */
    private Boolean checkInvoiceType(InvoiceBuyerRulesEntity info, InvoiceCollectionEntity bean) {
        if (CodeConstant.INVOICE_QB.equals(info.getBuyerTicketType())) {
            return Boolean.TRUE;
        }
        if (CodeConstant.INVOICE_PP.equals(info.getBuyerTicketType())) {
            if (CodeConstant.INVOICE_PP_LIST.contains(bean.getInvoiceType())) {
                return Boolean.TRUE;
            }
        }
        if (CodeConstant.INVOICE_ZP.equals(info.getBuyerTicketType())) {
            if (CodeConstant.INVOICE_ZP.equals(bean.getInvoiceType())) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }


    /**
     * 查询规则开启状态
     *
     * @param pid
     * @return 购方规则
     */
    private InvoiceBuyerRulesEntity getInfo(String pid) {
        InvoiceBuyerRulesEntity entity = buyerRulesMapper.selectOne(new QueryWrapper<InvoiceBuyerRulesEntity>().eq("uuid", pid));
        if (entity != null && CodeConstant.RULES_OPEN.equals(entity.getBuyerIsOpen())) {
            return entity;
        }
        return null;
    }
}
